// components/Blank/blank.js
var apiJs = require('../../utils/util.js');
var appData = getApp().globalData;
var dataParamList = [];
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    pageData: {
      type: Object
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    NowTime: apiJs.formatDate(new Date()),
    SelectIndex: 0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 绑定下拉选择事件
     */
    bindPickerChange: function(e) {
      var selectIndx = e.detail.value;
      this.setData({
        SelectIndex: parseInt(selectIndx)
      })
    },
    /**
     * 下拉选择时间事件
     */
    bindTimeChange: function(e) {
      var selectDate = e.detail.value;
      this.setData({
        NowTime: selectDate
      })
    },

    /**
     * 提交表单
     */
    FormSubmit: function(e) {
      dataParamList = [];
      var _this = this;
      var formId = e.detail.target.dataset.formid;
      var formParam = e.detail.value;
      var fields = _this.properties.pageData.Item.Fields;
      var isSubmit = false;
      var length = 0;
      if (fields != null && fields.length > 0) {
        for (var i = 0; i < fields.length; i++) {
          length = length + 1;
          if (fields[i].PrimaryName != 'PRI') {

            var param = new Object();
            var field = fields[i].FieldEName;
            var strName = fields[i].FieldCName;
            param.FileName = field;
            param.FileValue = formParam[field];

            if (fields[i].DataType == 'varchar') {
              if (fields[i].VarcharObj.Checks != null && fields[i].VarcharObj.Checks.length > 0) {
                //验证是否为必填
                if (fields[i].VarcharObj.Checks[0] == 1) {
                  if (param.FileValue == null || param.FileValue == '') {
                    var msg = '请输入';
                    wx.showModal({
                      title: '错误',
                      content: msg + fields[i].FieldCName,
                      showCancel: false
                    })
                    return;
                  }
                  if (param.FileValue.length > fields[i].VarcharObj.MaxLength || param.FileValue.length < fields[i].VarcharObj.MinLength) {
                    wx.showModal({
                      title: '错误',
                      content: strName + "长度不能大于" + fields[i].VarcharObj.MaxLength + "并且不能小于" + fields[i].VarcharObj.MinLength,
                      showCancel: false
                    })
                    isSubmit = false;
                    return;
                  } else
                    isSubmit = true;
                }else{
                  isSubmit = true;
                }

                //验证是否勾选不能和已有数据重复 
                if (fields[i].VarcharObj.Checks.length == 2) {
                  if (fields[i].VarcharObj.Checks[1] == 2) {
                    isSubmit = false;
                    wx.request({
                      url: appData.apiurl + "mobile/api/MiniForm/JudgeMiniFormData",
                      method: "get",
                      data: {
                        custId: appData.custID,
                        formId: formId,
                        paramObj: JSON.stringify(param)
                      },
                      header: {
                        "content-type": "application/json",
                        'X_ManuName': appData.manuName
                      },
                      success: function(res) {
                        if (res.data.code == 1200 && res.data.content != "通过验证") {
                          wx.showModal({
                            title: '错误',
                            content: "已有重复的" + strName + "，请重新输入",
                            showCancel: false
                          })
                          isSubmit = false;
                          return;
                        } else {
                          isSubmit=true;
                          if (isSubmit && length == fields.length) {
                            _this.SoureSubMit(_this, appData.custID, formId, dataParamList);
                          }
                        }
                      },
                      fail: function(res) {
                        wx.showModal({
                          title: '错误',
                          content: "已有重复的" + strName + "，请重新输入",
                          showCancel: false
                        })
                        isSubmit = false;
                        return;
                      }
                    })
                  }
                }else{
                  isSubmit = true;
                }
              }else{
                isSubmit = true;
              }
            }else{
              isSubmit=true;
            }
            dataParamList.push(param);
          }
        }
      }

      if (isSubmit && length == fields.length) {
        _this.SoureSubMit(_this, appData.custID, formId, dataParamList);
      }
    },

    SoureSubMit: function(_this, custId, formId, dataParamList) {
      wx.showLoading({
        title: '提交中...',
        mask: true
      });
      wx.request({
        url: appData.apiurl + "mobile/api/MiniForm/AddMiniFormData",
        method: "get",
        data: {
          custId: custId,
          formId: formId,
          dataParam: JSON.stringify(dataParamList)
        },
        header: {
          "content-type": "application/json",
          'X_ManuName': appData.manuName
        },
        success: function(res) {
          wx.hideLoading()
          if (res.data.code == 1200 && res.data.content != null) {
            wx.showToast({
              title: '提交成功',
              icon: 'success',
              duration: 2000
            });
          }
          _this.FormReset();
        },
        fail: function(res) {
          wx.hideLoading();
          wx.showModal({
            title: '错误',
            content: '提交失败',
            showCancel: false
          })
        }
      })
    },
    /**
     * 重置
     */
    FormReset: function() {
      console.log('form发生了reset事件')
    }
  }
})